package com.campus.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.shop.common.ConstantProperties;
import com.campus.shop.controller.web.vo.GoodsCartVo;
import com.campus.shop.controller.web.vo.OrderConfirmVo;
import com.campus.shop.entity.Goods;
import com.campus.shop.entity.ShopCart;
import com.campus.shop.mapper.GoodsMapper;
import com.campus.shop.mapper.ShopCartMapper;
import com.campus.shop.service.GoodsService;
import com.campus.shop.service.OrderService;
import com.campus.shop.service.ShopCartService;
import com.campus.shop.utils.ToolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author xr
 * @since 2023-12-362 15:13:50
 */
@Service
@Slf4j
public class ShopCartServiceImpl extends ServiceImpl<ShopCartMapper, ShopCart> implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;

    @Override
    public Integer getCartNum(Integer userId) {
        return shopCartMapper.getCartNum(userId, ConstantProperties.cartPayWaiting);
    }

    @Override
    public List<GoodsCartVo> getCartList(Boolean isCart, Integer userId) {
        List<GoodsCartVo> cartList = shopCartMapper.getCartList(userId, ConstantProperties.cartPayWaiting);
        for (GoodsCartVo goodsCartVo : cartList){
            goodsCartVo.setGoodsImg(ConstantProperties.hostName + goodsCartVo.getGoodsImg());
            BigDecimal totalPrice = goodsCartVo.getGoodsPrice().multiply(new BigDecimal(goodsCartVo.getGoodsNum()));
            goodsCartVo.setTotalPrice(totalPrice);
        }
        return cartList;
    }

    @Override
    public void changePayStatus(String ids, Integer payStatus) {
        Integer[] changeIds = ToolUtils.stringToArray(ids);
        shopCartMapper.changePayStatus(changeIds, payStatus);
    }

    @Override
    public List<OrderConfirmVo> getCartListById(Integer userId, String ids) {
        Integer[] idArray = ToolUtils.stringToArray(ids);
        List<OrderConfirmVo> orderConfirmVos = new ArrayList<>();
        List<GoodsCartVo> data = shopCartMapper.getCartListById(idArray, userId);
        for (GoodsCartVo goodsCartVo:data){
            goodsCartVo.setGoodsImg(ConstantProperties.hostName + goodsCartVo.getGoodsImg());
            BigDecimal totalPrice = goodsCartVo.getGoodsPrice().multiply(new BigDecimal(goodsCartVo.getGoodsNum()));
            goodsCartVo.setTotalPrice(totalPrice);
        }
        Map<Integer, List<GoodsCartVo>> groupedData = data.stream()
                .collect(Collectors.groupingBy(GoodsCartVo::getSaleId));
        // 遍历分组后的数据，计算每个分组的 sumPrice 和 list
        for (Map.Entry<Integer, List<GoodsCartVo>> entry : groupedData.entrySet()) {
            Integer saleId = entry.getKey();
            List<GoodsCartVo> goodsList = entry.getValue();
            BigDecimal sumPrice = goodsList.stream()
                    .map(GoodsCartVo::getTotalPrice)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            OrderConfirmVo orderConfirmVo = new OrderConfirmVo();
            orderConfirmVo.setSaleId(saleId);
            Optional<GoodsCartVo> firstGoodsCartVo = Optional.ofNullable(goodsList.get(0));
            String saleName = firstGoodsCartVo.map(GoodsCartVo::getSaleName).orElse(null);
            String storeName = firstGoodsCartVo.map(GoodsCartVo::getStoreName).orElse(null);
            orderConfirmVo.setSumPrice(sumPrice);
            orderConfirmVo.setSaleName(saleName);
            orderConfirmVo.setStoreName(storeName);
            orderConfirmVo.setList(goodsList);
            orderConfirmVos.add(orderConfirmVo);
        }
        return orderConfirmVos;

    }

    @Resource
    private GoodsMapper goodsMapper;
    @Transactional
    @Override
    public List<OrderConfirmVo>  getPayGoods(Integer userId, Integer goodsId) {
        GoodsCartVo goodInfo = goodsMapper.getGoodsInfo(goodsId);
        goodInfo.setGoodsImg(ConstantProperties.hostName + goodInfo.getGoodsImg());
        goodInfo.setTotalPrice(goodInfo.getGoodsPrice());
        goodInfo.setGoodsNum(1);
        OrderConfirmVo orderConfirmVo = new OrderConfirmVo();
        List<OrderConfirmVo> orderConfirmVos = new ArrayList<>();
        orderConfirmVo.setSaleId(0);
        orderConfirmVo.setStoreName(goodInfo.getStoreName());
        List<GoodsCartVo> goodsCartVos = new ArrayList<>();
        goodsCartVos.add(goodInfo);
        List<GoodsCartVo> goodsList = goodsCartVos;
        orderConfirmVo.setList(goodsList);
        orderConfirmVo.setSumPrice(goodInfo.getTotalPrice());
        orderConfirmVos.add(orderConfirmVo);
        return orderConfirmVos;
    }
}

